<!--
 * @Description: 设备管理
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: 清龙韵 1185716500@qq.com
 * @LastEditTime: 2025-09-26 15:32:09
-->
<template>
  <div>
    <!-- 第一行  -->
    <div class="header">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item>
          <el-select clearable v-model="form.online_status" placeholder="请选择设备状态">
            <el-option label="在线" :value="1"></el-option>
            <el-option label="离线" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select clearable v-model="form.bind_status" placeholder="请选择设备绑定状态">
            <el-option label="未绑定" :value="0"></el-option>
            <el-option label="已绑定" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input clearable v-model="form.name" placeholder="请输入设备名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toSearch">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 500px;" @click="toAdd">录入</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 第二行  -->
    <div class="table">
      <el-table :data="deviceData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="serial_number" label="编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="名称">
        </el-table-column>
        <el-table-column prop="online_status" label="设备状态">
          <template v-slot="scope">
            <div>
              <el-tag v-if="scope.row.online_status == 1" type="success">在线</el-tag>
              <el-tag v-else type="danger">离线</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="bind_status" label="绑定状态">
          <template v-slot="scope">
            <div>
              <el-tag v-if="scope.row.bind_status == 1" type="success">已绑定</el-tag>
              <el-tag v-else type="danger">未绑定</el-tag>
            </div>
          </template>
        </el-table-column>
         <el-table-column label="操作">
          <template v-slot="scope">
            <div>
              <el-button   type="primary"   @click="toEdit(scope.row)"  :underline="false">修改</el-button>
              <el-button :underline="false"  @click="toDelete(scope.row.id)" type="success" >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 抽屉 -->
    <el-drawer @close="reset" size="50%" :title="title" :visible.sync="visible" direction="rtl"
      custom-class="demo-drawer" ref="drawer">
      <div class="demo-drawer__content" style="padding-right: 10px;">
        <!-- {{ this.formSave }}  -->
        <el-form :rules="rules" ref="myForm" :model="formSave">   
           <el-row>
            <el-col :span="24">
              <el-form-item prop="serial_number"  placeholder="serial_number" label="编号">
                <el-input style="width: 90%;"  autocomplete="off" v-model="formSave.serial_number"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item   placeholder="name" type="text" label="名称">
                <el-input style="width: 90%;"  autocomplete="off" v-model="formSave.name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSubmit">确定</el-button>
        </div>
      </div>
    </el-drawer>

  </div>
</template>
<script>

import { get, del, post, postJSON } from '@/utils/request'
export default {
  data() {
    return {
      // 提供表单数据对象 --->条件对象 
      form: {},
      // 表格显示的数据 
      engData: [],
      // 设备数据 
      deviceData: [],
      params: {
        page: 1,
        pageSize: 10
      },
      // 修改表单数据对象 
      formSave: {},
       // 抽屉标题 
      title: '',
       // 控制抽屉显示与隐藏 
      visible: false,

       // 表单验证规则 
      rules: {
        serial_number : [
          { required: true, message: '请输入编号', trigger: "blur" },
        ],
        text: [
          { required: true, message: '请输入名称', trigger: "blur" },
        ],
        
      }
    }
  },
  methods: {

  // 修改工程信息  
    toEdit(row) {
      this.visible = true;
      // 抽屉标题 
      this.title = '修改设备信息',
      // ...拓展运算符  展开当前行对象
      // this.formSave = {...row};
      this.formSave = _.clone(row);
    },

    // 录入工程信息 
    toAdd() {
      // 抽屉显示
      this.visible = true;
      // 录入标题 
      this.title = "录入工程信息",
      // 清空表单数据对象 
      this.formSave = {};
    },

    //弹框
    // 去删除工程 
    toDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点了确定按钮 发起删除工程请求 
        let res = await del('/device/deleteById', { id });
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        // 从第一页重新载入数据 
        this.params.page = 1;
        // 重新载入数据 
        this.getDevData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
     // 获取工程数据方法 
    async getEngData() {
      let res = await get('/device/pageQuery', this.params);
      console.log(res, '获取响应');
      this.engData = res.data.list;
    },
 
   reset() {
      this.$refs['myForm'].resetFields();
    },

    toSearch() {
      this.params.page = 1;
      this.getDevData();
    },
     toSubmit() {
      this.$refs['myForm'].validate(async (valid) => {
        if (valid) {
          // 表单校验通过  发起保存或者更新请求 
          // alert('submit!');
          //  console.log(this.formSave);
          let data = _.clone(this.formSave);
          // data.address = data.address1.join(' ') + ' ' + data.address;
          // delete data.address1;
          // console.log(data, '-------------');
          let res = await postJSON('/device/saveOrUpdate', data);
          if (res.status == 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: 'success'
            });
            this.visible = false;
            // 重新查询数据
            this.getDevData();
          } else {
            return false;
          }
        } else {
          // 表单校验失败
          console.log('error submit!!');
          return false;
        }
      });
    },

    // 分页多条件获取所有设备数据 
    async getDevData() {
      let temp = {
        ...this.form,
        ...this.params
      };
      let res = await get('/device/pageQuery',temp);
      console.log(res, '获取设备响应');
      this.deviceData = res.data.list;

    }
  },
  created() {
    this.getDevData();
  }

}
</script>
<style lang="scss" scoped>
::v-deep .demo-drawer__footer{
  padding-top: 10px;
  border-top: 1px solid pink;
} </style>